using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Reflection;


public class GameConfigDataBase
{
		protected virtual string getFilePath ()
		{
				return "";
		}
        //PetConfigData
        static Dictionary<Type,Dictionary<string,GameConfigDataBase>> dataDic = new Dictionary<Type, Dictionary<string, GameConfigDataBase>> ();
        
    // 获取一条记录，key --> id
		public static T GetConfigData<T> (string key) where T:GameConfigDataBase
		{
				Type setT = typeof(T);
				if (!dataDic.ContainsKey (setT)) {
						ReadConfigData<T> ();
				}
				Dictionary<string,GameConfigDataBase> objDic = dataDic [setT];
                Debug.Log("test  (" + key + ")" + objDic.Count);//总共三条数据,objDic结构{key,PetConfigData}，key为每条petconfigdata中的id值
				if (!objDic.ContainsKey (key)) {
						throw new Exception ("no this config");
				}
				return (T)(objDic [key]);
		}
    //获取多条数据
		public static List<T> GetConfigDatas<T> () where T:GameConfigDataBase
		{
				List<T> returnList = new List<T> ();
				Type setT = typeof(T);
				if (!dataDic.ContainsKey (setT)) {
						ReadConfigData<T> ();
				}
                //将该T类型的数据保存到本字典中，方便读取
				Dictionary<string,GameConfigDataBase> objDic = dataDic [setT];
				foreach (KeyValuePair<string,GameConfigDataBase> kvp in objDic) {
						returnList.Add ((T)(kvp.Value));
				}
				return returnList;
		}

		static void ReadConfigData<T> () where T:GameConfigDataBase
		{
				T obj = Activator.CreateInstance<T> ();//创建实例
				string fileName = obj.getFilePath ();//PetConfigData pt = new PetConfigData();pt.getFilePath();

				string getString = Resources.Load< TextAsset>("GameConfig/" + fileName).text;

                CsvReaderByString csr = new CsvReaderByString (getString);

				Dictionary<string,GameConfigDataBase> objDic = new Dictionary<string, GameConfigDataBase> ();
				
				FieldInfo[] fis = new FieldInfo[csr.ColCount];
				for (int colNum=1; colNum<csr.ColCount+1; colNum++) {
						fis [colNum - 1] = typeof(T).GetField (csr [1, colNum]);//每个字段都设置进来
				}

				for (int rowNum=3; rowNum<csr.RowCount+1; rowNum++) {
						T configObj = Activator.CreateInstance<T> ();
						for (int i=0; i<fis.Length; i++) {
								string fieldValue = csr [rowNum, i + 1];
								object setValue = new object ();
								switch (fis [i].FieldType.ToString ()) {
								    case "System.Int32":
										    setValue = int.Parse (fieldValue);
										    break;
								    case "System.Int64":
										    setValue = long.Parse (fieldValue);
										    break;
								    case "System.String":
										    setValue = fieldValue;
										    break;
								    default:
										    Debug.Log ("error data type");
										    break;
								}
								fis [i].SetValue (configObj, setValue);//给每个实例中的变量设定值
								if (fis [i].Name == "key" || fis [i].Name == "id") {   
                                        //只检测key和id的值，然后添加到objDic 中
										objDic.Add (setValue.ToString (), configObj);//字典中{id值，T(数据，包含id数据)}，
                                        //利用该id值进行查找，与lua不同，lua中直接查找表里的id值，字典必须要找到key值
								}
						}
				}
				dataDic.Add (typeof(T), objDic);    //可以作为参数，将所有类型加到字典里，将类型跟数据加到字典中，方便多数据（excel）使用
		}
}
